﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.CTP.CommerceAPI.SDKSample.Client.ClientSetup;
using Microsoft.CTP.CommerceAPI.SDKSample.Client.Api;
using CommerceAccountProxy = Microsoft.Commerce.Proxy.AccountService;
using Microsoft.CTP.CommerceAPI.Proxy.v201112;
using System.Drawing;
using System.Xml;

namespace TestWebApplication
{
    public partial class showpurchase : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            CommerceAccountProxy.Identity requester = (CommerceAccountProxy.Identity)Session["requester"];
            string puid = requester.IdentityValue;
            string currentWLID = requester.IdentityProperty[0].Value;
            var utcNow = CTPClient.UtcNow();
            var contentHistory = ContentApi.GetContentPurchaseHistory(puid, currentWLID, utcNow.AddDays(-7), utcNow.AddMinutes(1), new PagingInfo { PageIndex = 0, PageSize = 100 });
            int i = 0;
            if (contentHistory != null)
                foreach (var content in contentHistory)
                {

                    TableRow row = new TableRow();
                    purchaseTable.Rows.Add(row);

                    TableCell cell = new TableCell();
                    cell.Text = content.LastUpdateDate.ToString();                   
                    row.Cells.Add(cell);

                    cell = new TableCell();
                    cell.Text = content.ContentName;
                    row.Cells.Add(cell);

                    cell = new TableCell();
                    cell.Text = content.CurrencyCode;
                    row.Cells.Add(cell);

                    cell = new TableCell();
                    cell.Text = content.TotalAmountWithoutTax.ToString();
                    row.Cells.Add(cell);

                    cell = new TableCell();
                    cell.Text = content.TotalTaxAmount.ToString();
                    row.Cells.Add(cell);

                    cell = new TableCell();
                    cell.Text = content.PurchaseStatus;
                    row.Cells.Add(cell);

                    cell = new TableCell();
                    Button button = new Button();
                    button.CommandArgument = i++ + "&" + content.TotalAmountWithoutTax;
                    button.BackColor = Color.Gainsboro;
                    button.BorderColor = Color.HotPink;
                    button.BorderStyle = BorderStyle.Solid;
                    button.BorderWidth = 1;
                    button.Text = "Full Refund";
                    button.Width = 80;
                    button.Height = 40;
                    button.Command += new CommandEventHandler(this.refund_Click);
                    cell.Controls.Add(button);
                    row.Cells.Add(cell);

                    row = new TableRow();
                    purchaseTable.Rows.Add(row);
                    cell = new TableCell();
                    cell.ColumnSpan = 7;
                    row.Cells.Add(cell);
                    Table table = new Table();
                    table.GridLines = GridLines.Both;
                    table.HorizontalAlign = HorizontalAlign.Center;
                    table.CellPadding = 15;
                    table.Width = Unit.Percentage(95);
                    cell.Controls.Add(table);

                    /*       foreach (OrderOutput order in info.OrderSet)
                           {
                               row = new TableRow();
                               table.Rows.Add(row);

                               cell = new TableCell();
                               cell.Text = order.OrderId.ToString();
                               row.Cells.Add(cell);

                               cell = new TableCell();
                               cell.Text = order.Currency;
                               row.Cells.Add(cell);

                               cell = new TableCell();
                               cell.Text = order.EstimatedChargeTime.ToString();
                               row.Cells.Add(cell);

                               cell = new TableCell();
                               cell.Text = order.BillingMode.ToString();
                               row.Cells.Add(cell);

                               cell = new TableCell();
                               cell.Text = order.Status;
                               row.Cells.Add(cell);

                               cell = new TableCell();
                               cell.Text = order.ItemEntries[0].UnitAmount.ToString();
                               row.Cells.Add(cell);


                           }*/
                }


        }
        protected void refund_Click(object sender, CommandEventArgs e)
        {
            string[] args = ((string)e.CommandArgument).Split('&');
            int index = int.Parse(args[0]);
            string amount = args[1];

            string accountID = (string)Session["accountID"];
            CommerceAccountProxy.Identity requester = (CommerceAccountProxy.Identity)Session["requester"];

            PurchaseHandler.refundPurchase(accountID, requester, index, amount);

            Response.Redirect("ShowPurchases.aspx");

        }
    }
}